<?php
/**
 * @package Plugin Unique Pages for Joomla! 2.5+
 * @version $Id: uniquepages.php 9 2013-01-03 20:01:36Z artur.ze.alves@gmail.com $
 * @author Artur Alves
 * @copyright (C) 2012 - Artur Alves
 * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
**/

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );

class plgSystemUniquePages extends JPlugin {

    function plgSystemUniquePages(& $subject, $config) {
        parent::__construct($subject, $config);
    }

    public function onAfterRender() {
		$app = JFactory::getApplication();
		if ($app->getClientId() === 0) {
			
	    	$active = JFactory::getApplication()->getMenu()->getActive();
		
			$value = " ";	
	    	if($this->params->get('menualias'))
				$value .= $active->alias;
				
			if($this->params->get('menuid'))
				$value .= " menu-" . $active->id;
				
			if($this->params->get('menualiasparents')) {
				$currentParent = JFactory::getApplication()->getMenu()->getItem($active->parent_id);

				if(isset($currentParent)) {
					do {
						$value .= " " . $currentParent->alias;
						
						if($currentParent->level > 1) {
							$currentParent = JFactory::getApplication()->getMenu()->getItem($currentParent->parent_id);
							$level = $currentParent->level;
						}
						else
							$level = 0;					
					}
					while ($level > 0);
				}
			}
			
			if($this->params->get('menutype'))
				$value .= " " .$active->menutype;
			 

			$html = JResponse::getBody();

			$dom = new DOMDocument;
			libxml_use_internal_errors(true);
			$dom->loadHTML($html);
			libxml_clear_errors();

			$bodys = $dom->getElementsByTagName("body");
			$body = $bodys->item(0);
			$body->setAttribute('class', $body->getAttribute('class') . $value);

			$injectedHTML = $dom->saveHTML();
			
			JResponse::setBody($injectedHTML);
		}
     	return true;   
    }
}	
?>
